angular.module('mamApp').controller('noticeController', ['$scope', 'NoticeManager','MessageManager', '$state', '$uibModal','ImsDialog', function ($scope, NoticeManager,MessageManager, $state, $uibModal,ImsDialog) {

    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        reviewStatus: $state.params.reviewStatus,
        keyword: $state.params.keyword,
        mode: $state.params.mode || 'inform',
    };



    $scope.keyword = $scope.search.keyword;

    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    }

    $scope.searchClick = function (keyword) {
        $scope.search.pageNo=1;
        $scope.search.keyword = keyword;
        $scope.search.reviewStatus = $scope.search.reviewStatus=='null'?null:$scope.search.reviewStatus;
        $state.go($state.current.name, $scope.search)

    }

    $scope.filterClick=function (mode) {
        $scope.search.mode=mode;
        $scope.search.pageNo=1;
        $scope.search.pageSize=10;
        $state.go($state.current.name, $scope.search)

    }
    $scope.noPermission=false;

    NoticeManager.noticeList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        if(result.data.c=='n'){
            $scope.noPermission=true;
        }else{
            $scope.noPermission=false;
            $scope.items = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;
        }
    })


    $scope.allCheckChange=function (allChecked) {
        $scope.items.forEach(function (item) {
            item.checked=allChecked;
        })
    }

    $scope.itemCheckChange=function (album) {
        $scope.allChecked=true;
        $scope.items.forEach(function (item) {
            if(!item.checked){
                $scope.allChecked=false;

            }
        })
    }

    $scope.deleteSelected=function(){
        var ids=[];
        $scope.items.forEach(function (item) {
            if(item.checked){
                ids.push(item.id)
            }
        })
        if(ids.length==0){
            return false;
        }
        ImsDialog.open({title:"消息删除",message:"确定删除所选的所有公告吗？"}).then(function (a) {
            if(a=="ok"){
                NoticeManager.noticeDelete(ids).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    }


   $scope.delete=function (item) {

       ImsDialog.open({title:"公告删除",message:"确定删除"+item.title+"？"}).then(function (a) {
           if(a=="ok"){

               NoticeManager.noticeDelete([item.id]).then(function (result) {
                   if(result.data.c=="s"){
                       $state.reload();
                   }
               })
           }
       })
   }

    $scope.keyup=function (e,keyword) {

        if(e.key=="Enter"){
            $scope.search.pageNo=1;
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)
        }
    }


}])

